package com.kongwg.java1;

import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author kongwg
 * @create 2020-12-23 18:07
 */
public class TCPTest3 {
    @Test
    public void client() {

        Socket socket = null;
        FileInputStream fis = null;
        OutputStream os = null;
        InputStream is=null;
        ByteArrayOutputStream baos = null;
        try {
            socket = new Socket("127.0.0.1", 8899);

            fis = new FileInputStream("爱情与友情.jpg");
            os = socket.getOutputStream();

            byte[] buffer = new byte[1024];

            int len;
            while ((len = fis.read(buffer)) != -1) {
                os.write(buffer);
            }

            socket.shutdownOutput();
             is = socket.getInputStream();
             baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[1024];
            int len1;
            while ((len1 = is.read(buffer1)) != -1){
                baos.write(buffer1,0,len1);
            }

            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @Test
    public void server() {

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos;
        ByteArrayOutputStream bos = null;
        OutputStream os= null;
        try {
            ss = new ServerSocket(8899);
            socket = ss.accept();

            is = socket.getInputStream();


            fos = new FileOutputStream("tcpTest2.jpg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);

            }

            //给客户端返回数据
             os = socket.getOutputStream();
            os.write("图片我收到了".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
